package com.taotao.rest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.utils.JsonUtils;
import com.taotao.rest.pojo.CatResult;
import com.taotao.rest.service.ItemCatService;

@Controller
//@ResponseBody
public class ItemCateController {

	@Autowired
	private ItemCatService itemCatService;

	/**
	 * 方法一，拼接字符串
	 */
	@RequestMapping(value = "/itemcat/all", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
	@ResponseBody
	public String getItemCateList(String callback) {
		CatResult itemCatList = itemCatService.getItemCatList();
		// 把pojo拼装成字符串
		String json = JsonUtils.objectToJson(itemCatList);
		String result = callback + "(" + json + ");";
		return result;
	}
	
	
	/**
	 * 方法二 使用MappingJacksonValue工具类
	 * 自动拼接
	 * 
	 * @param callback
	 * @return
	 */
	/*
	 * @RequestMapping(value="/itemcat/all",produces=MediaType.
	 * APPLICATION_JSON_VALUE + ";charset=utf-8")
	 * 
	 * @ResponseBody public Object getItemCateList(String callback) { CatResult
	 * itemCatList = itemCatService.getItemCatList(); MappingJacksonValue
	 * jacksonValue = new MappingJacksonValue(itemCatList);
	 * jacksonValue.setJsonpFunction(callback); return jacksonValue; }
	 */

}
